<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use App\Engine\BaseModel;
use App\Engine\TokenService;
use Illuminate\Database\Eloquent\Model;

/**
 * Class House
 *
 * @property int $id
 * @property int $pid
 * @property int $fid
 * @property string|null $name
 * @property string|null $banners
 * @property string|null $customs
 * @property string|null $icon
 * @property int $area
 * @property string|null $label
 * @property string|null $video
 * @property string|null $price
 * @property string|null $address
 * @property string|null $opening_quotations
 * @property string|null $house_types
 * @property string|null $phone
 * @property string|null $content
 * @property string|null $position
 * @property int $state
 * @property int $sort
 * @property string|null $custom_data
 * @property int $created_at
 * @property int $updated_at
 * @property string|null $highlights
 * @property string|null $short_name
 * @property string|null $advertisement
 * @property string|null $photoes
 * @property int $type
 * @property int $price_state
 *
 * @package App\Models
 */
class House extends BaseModel
{
	protected $table = 'houses';
	protected $casts = [
		'pid' => 'int',
		'fid' => 'int',
		'area' => 'int',
		'state' => 'int',
		'sort' => 'int',
		'created_at' => 'int',
		'updated_at' => 'int',
		'type' => 'int'
	];
	protected $fillable = [
		'pid',
		'fid',
		'name',
		'banners',
		'icon',
		'area',
		'label',
		'video',
		'price',
		'address',
		'opening_quotations',
		'house_types',
		'phone',
		'content',
		'position',
		'state',
		'sort',
		'custom_data',
		'highlights',
		'type',
        'customs',
        'short_name',
        'price_state',
        'advertisement',
        'photoes',
        'sale_infor',
        'album',
        'chick_nav',
        'pay_num',
        'vr_address'
	];
    public $rule=[
        'pid'=>'required',
        'fid'=>'required',
        'name'=>'required',
        'banners'=>'required',
        'icon'=>'required',
        'area'=>'required',
        'label'=>'required',
        'price'=>'required',
        'address'=>'required',
        'opening_quotations'=>'required',
        'house_types'=>'required',
        'phone'=>'required',
        'content'=>'required',
        'position'=>'required',
        'sort'=>'required',
        'type'=>'required',
    ];
    public $ruleMessage=[
        'type.required'=>"请选择类型",
        'sort.required'=>"请输入排序",
        'position.required'=>"请选择位置",
        'content.required'=>"请填写详情",
        'phone.required'=>"请输入电话",
        'house_types.required'=>"请添加户型",
        'opening_quotations.required'=>"请输入开盘时间",
        'address.required'=>"请输入地址",
        'price.required'=>"请输入价格",
        'label.required'=>"请填写标签",
        'area.required'=>"请输入面积",
        'icon.required'=>"请上传封面",
        'banners.required'=>"请上传轮播图",
        'name.required'=>"请输入名称",
        'fid.required'=>"请选择楼层",
        'pid.required'=>"请选择楼栋",
    ];

    public $with=["customlist","property","floors","types","around"];

    public function types()
    {
        return $this->belongsTo("App\Models\Type","type","id");
    }

    public function around()
    {
        return $this->hasMany("App\Models\AroundEnvironment","house_id","id");
    }

    public function collect()
    {
        return $this->belongsTo("App\Models\Collect","id","house_id")->where("uid",TokenService::getCurrentUid());
    }

    public function floors()
    {
        return $this->belongsTo("App\Models\Floor","fid","id");
    }

    public function property()
    {
        return $this->belongsTo("App\Models\property","pid","id");
    }

    public function __construct(array $attributes = [])
    {
        $this->with=["customlist"=>function($query){
            $query->with(["custom"]);
        }];

        parent::__construct($attributes);
    }

    public function customlist()
    {
        return $this->hasMany("App\Models\CustomHouse","house_id","id");
    }

    public function setPhotoesAttribute($value)
    {
        $this->attributes["photoes"]=$value!=''?json_encode($value,256):json_encode([],256);
    }

    public function getPhotoesAttribute($value)
    {
        return $value!=''?json_decode($value,256):[];
    }

    public function getAlbumAttribute($value)
    {
        return $value!=''?json_decode($value,256):[];
    }

    public function setAlbumAttribute($value)
    {
        $this->attributes["album"]=$value!=''?json_encode($value,256):json_encode([],256);
    }


	public function setCustomsAttribute($value)
    {
        if (!$value){
            $this->attributes["customs"]=$value;
        }
        $this->attributes["customs"]=json_encode($value,256);
    }
    public function getCustomsAttribute($value)
    {
        if (!$value){
            return [];
        }
       return  json_decode($value,256);
    }
    public function setBannersAttribute($value)
    {
        if ($value)
            $this->attributes["banners"]=json_encode($value,256);
        else
            $this->attributes["banners"]=json_encode([],256);
    }
    public function getBannersAttribute($value)
    {
        if (!$value)
            return $value;
        return  json_decode($value,256);
    }
    public function setHouseTypesAttribute($value)
    {
        if ($value){
            $this->attributes["house_types"]=json_encode($value,256);
        }
        else
            $this->attributes["house_types"]=json_encode([],256);
    }
    public function getHouseTypesAttribute($value)
    {
        return json_decode($value,256);
    }

    public function setCustomDataAttribute($value)
    {
        if ($value){
            $this->attributes["custom_data"]=json_encode($value,256);
        }else
            $this->attributes["custom_data"]=json_encode([],256);
    }

    public function getCustomDataAttribute($value)
    {
        return json_decode($value,256);
    }

    public function setHighlightsAttribute($value)
    {
        if ($value){
            $this->attributes["highlights"]=json_encode($value,256);
        }else
            $this->attributes["highlights"]=json_encode([],256);
    }

    public function getHighlightsAttribute($value)
    {
        return json_decode($value,256);
    }
}
